### Script for submission ###
### Please do not cite or circulate without the author's permission ###

# Descriptive Statistics and Data Cleaning

install.packages("readxl")
install.packages("dplyr")

library("readxl")
library("dplyr")
library("ggplot2")
library(QCA)
library(SetMethods)

data<-read_excel("conditionaldata_set.xlsx")
str(data)
head(data)
summary(data)

# To add row names:

pnames <- c("BIH99",
            "HRV99",
            "MNE99",
            "BIH00",
            "HRV00",
            "MNE00",
            "BIH01",
            "HRV01",
            "MNE01",
            "BIH02",
            "HRV02",
            "MNE02",
            "BIH03",
            "HRV03",
            "MNE03",
            "BIH04",
            "HRV04",
            "MNE04",
            "BIH05",
            "HRV05",
            "MNE05",
            "BIH06",
            "HRV06",
            "MNE06",
            "BIH07",
            "HRV07",
            "MNE07",
            "BIH08",
            "MNE08",
            "BIH09",
            "MNE09",
            "BIH10",
            "MNE10",
            "BIH11",
            "MNE11",
            "BIH12",
            "MNE12",
            "BIH13",
            "MNE13",
            "BIH14",
            "MNE14",
            "BIH15",
            "MNE15")

data <- as.data.frame(data)
row.names(data) <- pnames
data$Observations <- NULL

## Descriptive Statistics: NumberofMFIS (BiH=18, HRV=3, MNE=4)
## Domestic Support: Present: 1, Absent: 0
## Throughout the country: Yes:1, No:0
## ggplot(data, aes(x=YEAR,y=MFIS,group=COUNTRY))+geom_line(aes(col=COUNTRY))+facet_wrap(.~COUNTRY) + theme_light()

##Calibrating Number of MFIS: (Set: High Number of MFIS,existing for most of the years studied)
str(data$NumberofMFIS)
data$NumberofMFISb<-as.numeric(data$NumberofMFIS)
str(data$NumberofMFISb)
summary(data$NumberofMFISb)
hist(data$NumberofMFISb)
data$NMFIS<-calibrate(data$NumberofMFISb, type="crisp",thresholds = 4)
print(data[ ,c("NumberofMFISb","NMFIS")])

## Calibrating Government Support for MFIS: Present (1), Absent (0)
str(data$GovtSupportMFIS)
data$GovtSupportMFISb<-as.numeric(data$GovtSupportMFIS)
str(data$GovtSupportMFISb)
summary(data$GovtSupportMFISb)
data$GSMFIS<- calibrate(data$GovtSupportMFISb, type="crisp",thresholds = 1)
data$GSMFIS
print(data[ ,c("GovtSupportMFISb","GSMFIS")])


##Calibrating MFIS Present Throughout Country (ThroughoutCountry)
str(data$ThroughoutCountry)
data$ThroughoutCountryb<-as.numeric(data$ThroughoutCountry)
str(data$ThroughoutCountryb)
summary(data$ThroughoutCountryb)
data$TCMFIS<- calibrate(data$ThroughoutCountryb, type="crisp",thresholds = 1)
data$TCMFIS
hist(data$TCMFIS)
print(data[ ,c("ThroughoutCountryb","TCMFIS")])

## Creating a macro-condition (High Presence of Microfinance)
data$HPM<-fuzzyand(data$NMFIS,data$GSMFIS,data$TCMFIS)
print(data[ ,c("HPM","NMFIS","GSMFIS","TCMFIS")])
skew.check(data$HPM)

## Calibrating European Union Membership, Set: Full Members, Non-Members: 0, Potential Candidates:0.25, Candidate Countries: 0.67; Full members:1
str(data$EUM)
data$EUMB<-calibrate(data$EUM, thresholds = "e=0,c=0.9,i=2")
print(data[ ,c("EUM","EUMB")])
skew.check(data$EUMB)
plot(data$EUM,data$EUMB)

## Calibrating International Human Rights:
str(data$INTHRT)
sort(data$INTHRT)
Xplot(data$INTHRT)
plot(data$INTHRT,data$HRTINT)
quantHRT<-quantile(data$INTHRT,c(0.2,0.4,0.5,0.6,0.8))
quantHRT
data$HRTB<-NA
data$HRTB[data$INTHRT <= quantHRT[1]] <- 0
data$HRTB[data$INTHRT > quantHRT[1] & data$INTHRT<= quantHRT[2]] <- 0.2
data$HRTB[data$INTHRT> quantHRT[2] & data$INTHRT<= quantHRT[3]] <- 0.4
data$HRTB[data$INTHRT> quantHRT[3] & data$INTHRT<= quantHRT[4]] <- 0.6
data$HRTB[data$INTHRT> quantHRT[4] & data$INTHRT<= quantHRT[5]] <- 0.8
data$HRTB[data$INTHRT> quantHRT[5]] <- 1
data$HRTBINOM <- indirectCalibration(data$INTHRT,data$HRTB,binom=TRUE)
data$HRTBETA <- indirectCalibration(data$INTHRT,data$HRTB,binom=FALSE)
cor(data$HRTBINOM,data$HRTBETA)
plot(data$INTHRT,data$HRTBINOM, col="red")
plot(data$INTHRT,data$HRTBETA,col="blue")
skew.check(data$HRTBINOM)
skew.check(data$HRTBETA)

# Calibrating GDP: (Indirect Calibration)
ggplot(data, aes(x=YEAR,y=GDP,group=COUNTRY))+geom_line(aes(col=COUNTRY)) + theme_light()
print(data$GDP)
quantgdp <- quantile(data$GDP,c(0.2,0.4,0.5,0.6,0.8))
quantgdp
data$GDPB<-NA
data$GDPB[data$GDP <= quantgdp[1]] <- 0
data$GDPB[data$GDP > quantgdp [1] & data$GDP <= quantgdp[2]] <- 0.2
data$GDPB[data$GDP > quantgdp [2] & data$GDP <= quantgdp[3]] <- 0.4
data$GDPB[data$GDP > quantgdp [3] & data$GDP <= quantgdp[4]] <- 0.6
data$GDPB[data$GDP > quantgdp [4] & data$GDP <= quantgdp[5]] <- 0.8
data$GDPB[data$GDP > quantgdp [5]] <- 1
data$GDPBINOM <- indirectCalibration(data$GDP,data$GDPB,binom=TRUE)
data$GDPBETA <- indirectCalibration(data$GDP,data$GDPB,binom=FALSE)
cor(data$GDPBINOM,data$GDPBETA)
plot(data$GDP,data$GDPBINOM, col="red")
plot(data$GDP,data$GDPBETA,col="blue")
skew.check(data$GDPBINOM)
skew.check(data$GDPBETA)
Xplot(data$GDP, jitter=TRUE)

## "autocracies" (-10 to -6), "anocracies" (-5 to +5 and three special values: -66, -77 and -88), and "democracies" (+6 to +10)
# Calibrating Democracy:
ggplot(data, aes(x=YEAR,y=DEM,group=COUNTRY))+geom_line(aes(col=COUNTRY))+facet_wrap(.~COUNTRY) + theme_light()
print(data$DEM)
str(data$DEM)
hist(data$DEM)
data$DEMC<-calibrate(data$DEM, thresholds="e=-66,c=-1,i=6")
print(data[ ,c("DEM","DEMC")])
hist(data$DEMC)

# Calibrating PCL: Set of high political and civil liberty
ggplot(data, aes(x=YEAR,y=PCL, group=COUNTRY)) + geom_line(aes(col=COUNTRY)) + theme_light()
print(data$PCL)
data$CPLC<-calibrate(data$PCL, thresholds="i=1.9,c=3.9,e=4.9")
print(data[ ,c("PCL","CPLC")])
plot(data$PCL,data$CPLC)

## Creating a macro-condition: Set of high access to political freedom
data$HPF<-fuzzyand(data$DEMC,data$CPCL)
hist(data$HPF)
skew.check(data$HPF)

## Calibrating LTRCY:
print(data$LTRCY)
ggplot(data, aes(x=YEAR,y=LTRCY, group=COUNTRY)) + geom_line(aes(col=COUNTRY)) + theme_light()
Xplot(data$LTRCY, jitter=TRUE)
quantlitc <- quantile(data$LTRCY,c(0.2,0.4,0.5,0.6,0.8))
quantlitc
data$LITB<-NA
data$LITB[data$LTRCY <= quantlitc [1]]<-0
data$LITB[data$LTRCY > quantlitc [1] & data$LTRCY <= quantlitc [2]]<-0.2
data$LITB[data$LTRCY > quantlitc [2] & data$LTRCY <= quantlitc [3]]<-0.4
data$LITB[data$LTRCY > quantlitc [3] & data$LTRCY <= quantlitc [4]]<-0.6
data$LITB[data$LTRCY > quantlitc [4] & data$LTRCY <= quantlitc [5]]<-0.8
data$LITB[data$LTRCY > quantlitc [5]]<-1
data$LITbinom <- indirectCalibration(data$LTRCY, data$LITB, binom=TRUE)
data$LITbeta <- indirectCalibration(data$LTRCY, data$LITB, binom=FALSE)
skew.check(data$LITbinom)
skew.check(data$LITbeta)
cor(data$LITbinom,data$LITbeta)
plot(data$LTRCY,data$LITbinom, col="red")
plot(data$LTRCY,data$LITbeta,col="blue")


# Condition: LIFEX, To calibrate Life Expectancy:
print(data$LIFEX)
ggplot(data, aes(x=YEAR,y=LIFEX, group=COUNTRY)) + geom_line(aes(col=COUNTRY)) + theme_light()
Xplot(data$LIFEX, jitter=TRUE, at=pretty(data$LIFEX))
quantlife <- quantile(data$LIFEX,c(0.2,0.4,0.5,0.6,0.8))
quantlife
data$LIFEXB<-NA
data$LIFEXB[data$LIFEX <= quantlife [1]]<-0
data$LIFEXB[data$LIFEX > quantlife [1] & data$LIFEX <= quantlife [2]]<-0.2
data$LIFEXB[data$LIFEX > quantlife [2] & data$LIFEX <= quantlife [3]]<-0.4
data$LIFEXB[data$LIFEX > quantlife [3] & data$LIFEX <= quantlife [4]]<-0.6
data$LIFEXB[data$LIFEX > quantlife [4] & data$LIFEX <= quantlife [5]]<-0.8
data$LIFEXB[data$LIFEX > quantlife [5]]<-1
data$LIFEXbinom <- indirectCalibration(data$LIFEX,data$LIFEXB,binom=TRUE)
data$LIFEXbeta <- indirectCalibration(data$LIFEX,data$LIFEXB, binom=FALSE)
cor(data$LIFEXbinom,data$LIFEXbeta)
plot(data$LIFEX,data$LIFEXbinom, col="red")
plot(data$LIFEX,data$LIFEXbeta,col="blue")
skew.check(data$LIFEXbeta)
skew.check(data$LIFEXbinom)

## Creating a new macro-condition: access to high social opportunities
data$HSO<-fuzzyand(data$LITbeta,data$LIFEXbeta)
skew.check(data$HSO)
hist(data$HSO)

## Calibrating access to high transparency guarantees:
# Control_of_Corruption 
# Researchers started collecting this data from the year 1996
# Hence, I have coded the year 1995 as zero
# Between the years 1996-2001, data was collected for every alternate year.
# Hence, I have recoded 1997, 1999, 2001 with the values of the year before.
# Between the years 2002-2015, data is available for every year
# Due to the presence of missing values (NAs), R has characterized this variable 
# as a character variable. It needs to be a numeric variable first before recoding.

data$NCOR<-as.numeric(data$FCOR)
data$NCOR[which(data$COUNTRY=="BiH" & data$YEAR %in% c(1999))]<-42.783499999999997
data$NCOR[which(data$COUNTRY=="BiH" & data$YEAR %in% c(2001))]<-33.502540000000003
data$NCOR[which(data$COUNTRY=="HRV" & data$YEAR %in% c(1999))]<-34.536079999999998
data$NCOR[which(data$COUNTRY=="HRV" & data$YEAR %in% c(2001))]<-56.345179999999999
data$NCOR[which(data$COUNTRY=="MNE" & data$YEAR %in% c(1999))]<-71.134020000000007
data$NCOR[which(data$COUNTRY=="MNE" & data$YEAR %in% c(2001))]<-53.299489999999999
print(data$NCOR)
ggplot(data, aes(x=YEAR,y=NCOR, group=COUNTRY)) + geom_line(aes(col=COUNTRY)) + theme_light()
Xplot(data$NCOR,jitter=TRUE,at=pretty(data$NCOR))
quantcor <- quantile(data$NCOR,c(0.2,0.4,0.5,0.6,0.8))
quantcor
data$NCORb<-NA
data$NCORb[data$NCOR <= quantcor[1]]<-0
data$NCORb[data$NCOR > quantcor [1] & data$NCOR <= quantcor [2]] <-0.2
data$NCORb[data$NCOR > quantcor [2] & data$NCOR <= quantcor [3]]<-0.4
data$NCORb[data$NCOR > quantcor [3] & data$NCOR <= quantcor [4]]<-0.6
data$NCORb[data$NCOR > quantcor [4] & data$NCOR <= quantcor [5]]<-0.8
data$NCORb[data$NCOR > quantcor [5]]<-1
data$NCORbinom<-indirectCalibration(data$NCOR,data$NCORb,binom=TRUE) 
data$NCORbeta<-indirectCalibration(data$NCOR,data$NCORb,binom=FALSE) 
cor(data$NCORbinom,data$NCORbeta)
plot(data$NCOR,data$NCORbinom, col="red")
plot(data$NCOR,data$NCORbeta,col="blue")
skew.check(data$NCORbeta)
skew.check(data$NCORbinom)

# Condition: FEFFECT
# Government effectiveness 
# The data is in percentage format
# Researchers started collecting this data from the year 1996
# Hence, I have coded the year 1995 as zero
# Between the years 1996-2001, data was collected for every alternate year.
# Hence, I have recoded 1997, 1999, 2001 with the values of the year before.
# Between the years 2002-2015, data is available for every year
# Due to the presence of missing values (NAs), R has characterized the variable 
# as a character variable. It needs to be a numeric variable first before recoding.

data$NEFFECT<- as.numeric(data$FEFFECT)
data$NEFFECT[which(data$COUNTRY=="BiH" & data$YEAR %in% c(1999))]<-11.398960000000001
data$NEFFECT[which(data$COUNTRY=="MNE" & data$YEAR %in% c(1999,2000,2001,2002,2003,2004))]<-0
data$NEFFECT[which(data$COUNTRY=="BiH" & data$YEAR %in% c(2001))]<-17.948720000000002
data$NEFFECT[which(data$COUNTRY=="HRV" & data$YEAR %in% c(1999))]<-59.58549
data$NEFFECT[which(data$COUNTRY=="HRV" & data$YEAR %in% c(2001))]<-66.666659999999993
print(data$NEFFECT)
ggplot(data, aes(x=YEAR,y=NEFFECT, group=COUNTRY)) + geom_line(aes(col=COUNTRY)) + theme_light()
Xplot(data$NEFFECT, jitter=TRUE)
quanteffect <- quantile(data$NEFFECT,c(0.2,0.4,0.5,0.6,0.8))
quanteffect
data$NEFFECTB<-NA
data$NEFFECTB[data$NEFFECT <= quanteffect [1]]<-0
data$NEFFECTB[data$NEFFECT > quanteffect [1] & data$NEFFECT <= quanteffect [2]] <-0.2
data$NEFFECTB[data$NEFFECT > quanteffect [2] & data$NEFFECT <= quanteffect [3]]<-0.4
data$NEFFECTB[data$NEFFECT > quanteffect [3] & data$NEFFECT <= quanteffect [4]]<-0.6
data$NEFFECTB[data$NEFFECT > quanteffect [4] & data$NEFFECT <= quanteffect [5]]<-0.8
data$NEFFECTB [data$NEFFECT > quanteffect [5]]<-1
data$NEFFECTbinom<-indirectCalibration(data$NEFFECT,data$NEFFECTB,binom=TRUE) 
data$NEFFECTbeta<-indirectCalibration(data$NEFFECT,data$NEFFECTB,binom=FALSE) 
cor(data$NEFFECTbinom,data$NEFFECTbeta)
plot(data$NEFFECT,data$NEFFECTbinom, col="red")
plot(data$NEFFECT,data$NEFFECTbeta,col="blue")
skew.check(data$NEFFECTbinom)
skew.check(data$NEFFECTbeta)


# Calibrating Female Own-Account Workers
Xplot(data$OWN, jitter=TRUE, at=pretty(data$OWN))
sort(data$OWN)
quantfown <- quantile(data$OWN,c(0.2,0.4,0.5,0.6,0.8))
quantfown
data$OWNB<-NA
data$OWNB[data$OWN<= quantfown [1]]<-0
data$OWNB[data$OWN > quantfown [1] & data$OWN <= quantfown [2]]<-0.2
data$OWNB[data$OWN > quantfown [2] & data$OWN <= quantfown [3]]<-0.4
data$OWNB[data$OWN > quantfown [3] & data$OWN <= quantfown [4]]<-0.6
data$OWNB[data$OWN > quantfown [4] & data$OWN <= quantfown [5]]<-0.8
data$OWNB[data$OWN > quantfown [5]]<-1
data$OWNbinom <- indirectCalibration(data$OWN, data$OWNB, binom=TRUE)
data$OWNbeta <- indirectCalibration(data$OWN, data$OWNB, binom=FALSE)
cor(data$OWNbinom,data$OWNbeta)
plot(data$OWN,data$OWNbinom, col="red")
plot(data$OWN,data$OWNbeta,col="blue")
skew.check(data$OWNbinom)
skew.check(data$OWNbeta)

## Calibrating female family workers:
Xplot(data$FAM, jitter=TRUE, at=pretty(data$FAM))
sort(data$FAM)
quantffam <- quantile(data$FAM,c(0.2,0.4,0.5,0.6,0.8))
quantffam
data$FAMcal<-NA
data$FAMcal[data$FAM <= quantffam [1]]<-0
data$FAMcal[data$FAM > quantffam [1] & data$FAM <= quantffam [2]]<-0.2
data$FAMcal[data$FAM > quantffam [2] & data$FAM <= quantffam [3]]<-0.4
data$FAMcal[data$FAM > quantffam [3] & data$FAM <= quantffam [4]]<-0.6
data$FAMcal[data$FAM > quantffam [4] & data$FAM <= quantffam [5]]<-0.8
data$FAMcal[data$FAM > quantffam [5]]<-1
data$FAMcalbinom <- indirectCalibration(data$FAM, data$FAMcal, binom=TRUE)
data$FAMcalbeta <- indirectCalibration(data$FAM, data$FAMcal, binom=FALSE)
cor(data$FAMcalbinom,data$FAMcalbeta)
plot(data$FAM,data$FAMcalbinom, col="red")
plot(data$FAM,data$FAMcalbeta,col="blue")
skew.check(data$FAMcalbinom)
skew.check(data$FAMcalbeta)

## Calibrating female employees:
Xplot(data$EMPE, jitter=TRUE, at=pretty(data$EMPE))
sort(data$EMPE)
quantfemp <- quantile(data$EMPE,c(0.2,0.4,0.5,0.6,0.8))
quantfemp
data$EMPEcal<-NA
data$EMPEcal[data$EMPE <= quantfemp [1]]<-0
data$EMPEcal[data$EMPE > quantfemp [1] & data$EMPE <= quantfemp [2]]<-0.2
data$EMPEcal[data$EMPE > quantfemp [2] & data$EMPE <= quantfemp [3]]<-0.4
data$EMPEcal[data$EMPE > quantfemp [3] & data$EMPE <= quantfemp [4]]<-0.6
data$EMPEcal[data$EMPE > quantfemp [4] & data$EMPE <= quantfemp [5]]<-0.8
data$EMPEcal[data$EMPE > quantfemp [5]]<-1
data$EMPEbinom <- indirectCalibration(data$EMPE, data$EMPEcal, binom=TRUE)
data$EMPEbeta <- indirectCalibration(data$EMPE, data$EMPEcal, binom=FALSE)
cor(data$EMPEbinom,data$EMPEbeta)
plot(data$EMPE,data$EMPEbinom, col="red")
plot(data$EMPE,data$EMPEbeta,col="blue")
skew.check(data$EMPEbinom)
skew.check(data$EMPEbeta)

## Calibrating female employers:
Xplot(data$EMPR,jitter=TRUE, at=pretty(data$EMPR))
sort(data$EMPR)
quantfempr <- quantile(data$EMPR,c(0.2,0.4,0.5,0.6,0.8))
quantfempr
data$EMPRcal<-NA
data$EMPRcal[data$EMPR <= quantfempr [1]]<-0
data$EMPRcal[data$EMPR > quantfempr [1] & data$EMPR <= quantfempr [2]]<-0.2
data$EMPRcal[data$EMPR > quantfempr [2] & data$EMPR <= quantfempr [3]]<-0.4
data$EMPRcal[data$EMPR > quantfempr [3] & data$EMPR <= quantfempr [4]]<-0.6
data$EMPRcal[data$EMPR > quantfempr [4] & data$EMPR <= quantfempr [5]]<-0.8
data$EMPRcal[data$EMPR > quantfempr [5]]<-1
data$EMPRbinom <- indirectCalibration(data$EMPR, data$EMPRcal, binom=TRUE)
data$EMPRbeta <- indirectCalibration(data$EMPR, data$EMPRcal, binom=FALSE)
cor(data$EMPRbinom,data$EMPRbeta)
plot(data$EMPR,data$EMPRbinom, col="red")
plot(data$EMPR,data$EMPRbeta,col="blue")
skew.check(data$EMPRbinom)
skew.check(data$EMPRbeta)

# Condition: EU Membership
print(data$EUM)
ggplot(data, aes(x=YEAR,y=EUM, group=COUNTRY)) + geom_line(aes(col=COUNTRY))+ facet_wrap(.~COUNTRY) + theme_light()

# Condition: Human Rights Treaties
print(data$HRT)
ggplot(data, aes(x=YEAR,y=HRT, group=COUNTRY)) + geom_line(aes(col=COUNTRY))+ facet_wrap(.~COUNTRY) + theme_light()

# Outcome: Female Own-Account Workers
print(data$OWN)
ggplot(data, aes(x=YEAR,y=OWN, group=COUNTRY)) + geom_line(aes(col=COUNTRY)) + theme_light()

#Outcome: Female Family Workers
print(data$FAM)
ggplot(data, aes(x=YEAR, y=FAM, group=COUNTRY)) + geom_line(aes(col=COUNTRY)) + theme_light()

# Outcome: Female Employees
print(data$EMPE)
ggplot(data, aes(x=YEAR, y=EMPE, group=COUNTRY)) + geom_line(aes(col=COUNTRY)) + theme_light()

# Outcome: Female Employers
print(data$EMPR)
ggplot(data, aes(x=YEAR, y=EMPR, group=COUNTRY)) + geom_line(aes(col=COUNTRY)) + theme_light()


## To create a new dataset:
panel<-data %>% select(HPM,GDPBETA,HPF,HSO,NCORbeta,NEFFECTbeta,EUMB,HRTBETA,OWNbeta,FAMcalbeta,EMPEbeta,EMPRbeta,COUNTRY,YEAR)
write.csv(panel, file="panel2.csv")
panel<-read.csv("panel2.csv")


panel$EUMB->panel$EUM
panel$HRTBETA->panel$HRT
panel$GDPBETA->panel$GDP
panel$NCORbeta->panel$NCOR
panel$NEFFECTbeta->panel$NEFFECT
panel$OWNbeta->panel$OWN
panel$FAMcalbeta->panel$FAM
panel$EMPEbeta->panel$EMPE
panel$EMPRbeta->panel$EMPR

#Creating a macro-condition: Increasing number of women employed at the national economy
panel$NWE<-fuzzyand(panel$OWN,panel$FAM,panel$EMPE,panel$EMPR)
print(panel[ ,c("OWN","FAM","EMPE","EMPR","NWE")])
skew.check(panel$NWE)

# To check for necessary conditions: Presence of Outcome (NWE)

pof("HPM <= NWE", data=panel, relation = "necessity")
pof("EUM <= NWE", data=panel, relation = "necessity")
pof("HRT <= NWE", data=panel, relation="necessity")
pof("GDP <= NWE", data=panel, relation="necessity")
pof("HPF <= NWE", data=panel, relation = "necessity")
pof("HSO <= NWE", data=panel, relation="necessity")
pof("NCOR <= NWE", data=panel, relation = "necessity")
pof("NEFFECT <= NWE", data=panel, relation = "necessity")

# To check for necessary conditions: Absence of Outcome (NWE)

pof("~HPM <= NWE", data=panel, relation = "necessity")
pof("~EUM <= NWE", data=panel, relation = "necessity")
pof("~HRT <= NWE", data=panel, relation="necessity")
pof("~GDP <= NWE", data=panel, relation="necessity")
pof("~HPF <= NWE", data=panel, relation = "necessity")
pof("~HSO <= NWE", data=panel, relation="necessity")
pof("~NCOR <= NWE", data=panel, relation = "necessity")
pof("~NEFFECT <= NWE", data=panel, relation = "necessity")

## To check for sufficient conditions:
ttNWE <- truthTable(panel, conditions= "HPM,EUM,HRT,GDP,HPF,HSO,NCOR,NEFFECT",outcome="NWE", incl.cut = 0.99, sort.by = "incl,n", dcc= TRUE, decreasing=FALSE,complete=TRUE, show.cases = TRUE)
ttNWE

# For the conservative solution of the sufficiency result:
sol_NWE <- minimize(ttNWE, details=TRUE)
sol_NWE
sol_NWE$PIchart
pimplot(data=panel, results = sol_NWE, incl.tt = 0.99, outcome = "NWE", jitter=TRUE)

# For the parsimonious solution of the sufficiency result:
sol_NWEP <- minimize(ttNWE, include = "?", details=TRUE)
sol_NWEP
names(sol_NWEP)
sol_NWEP$PIchart
sol_NWEP$SA
pimplot(data=panel, results = sol_NWEP, incl.tt = 0.99, outcome = "NWE", jitter=TRUE)

# For the intermediate solution and its logical minimization:
sol_NWEI <- minimize(ttNWE, include = "?", dir.exp = "1,1,1,1,1,1,1,1",details=TRUE)
sol_NWEI
names(sol_NWEI$i.sol)

# Standard Analysis:
sol_NWEI$i.sol$C1P1$EC
sol_NWEI$i.sol$C1P1$DC
pimplot(data=panel, results = sol_NWEI, incl.tt = 0.99, outcome = "NWE", jitter=TRUE)

# For the enhanced standard analysis:
ESANWE <- findRows(obj = ttNWE, type = 2)
ESANWE
ESANWEB <- findRows(obj=ttNWE, type = 3)
ESANWEB
#ESA<-c(ESAOW,ESAOWB)
#ESA

# For the enhanced parsimonious solution:
sol_NWESA <- minimize(ttNWE, include = "?", exclude=ESANWE, details=TRUE)
sol_NWESA

# For the enhanced intermediate solution:
sol_NWEIA <- minimize(ttNWE, include = "?", exclude= ESANWE, dir.exp ="1,1,1,1,1,1,1,1",details=TRUE)
sol_NWEIA
pimplot(data=panel, results = sol_NWEIA, incl.tt = 0.99, outcome = "NWE", jitter=TRUE)

# For the logical minimization of the enhanced intermediate solution:
names(sol_NWEIA)
names(sol_NWEIA$i.sol)

##To create a new dataset:
posNWED <- sol_NWEIA$i.sol$C1P1$pims
posNWED
posNWEDB<-sol_NWEIA$i.sol$C1P3$pims
posNWEDB
smmr(results=sol_NWEIA,outcome="NWE", match=FALSE, cases=2,term=1)

#Creating a new dataset:
panelNWE<- cbind(panel, posNWED,posNWEDB)

# For the sufficiency analysis:
NWESA<-cluster(data = panelNWE, results = "HPM*EUM*~HPF*~HSO", outcome = "NWE",unit_id = "COUNTRY", cluster_id = "YEAR", wicons=TRUE)
NWESA
cluster.plot(cluster.res=NWESA,labs=TRUE,size=8,angle=6,wicons=TRUE)
NWESAB<-cluster(data = panelNWE, results = "~HPM*~EUM*GDP*HPF*NEFFECT", outcome = "NWE",unit_id = "COUNTRY", cluster_id = "YEAR", wicons=TRUE)
NWESAB
NWESAC<-cluster(data = panelNWE, results = "HPM*EUM*HRT*GDP*~HPF*NCOR", outcome = "NWE",unit_id = "COUNTRY", cluster_id = "YEAR", wicons=TRUE)
NWESAC
NWESAD<-cluster(data = panelNWE, results = "HPM*EUM*~HSO*~NCOR", outcome = "NWE",unit_id = "COUNTRY", cluster_id = "YEAR", wicons=TRUE)
NWESAD
NWESAE<-cluster(data = panelNWE, results = "~HPM*~EUM*GDP*HPF*NEFFECT", outcome = "NWE",unit_id = "COUNTRY", cluster_id = "YEAR", wicons=TRUE)
NWESAE
NWESAF<-cluster(data = panelNWE, results = "HPM*EUM*HRT*GDP*~HPF*HSO*NCOR", outcome = "NWE",unit_id = "COUNTRY", cluster_id = "YEAR", wicons=TRUE)
NWESAF

t<-"HPM*GDP*HPF*HSO*NCOR*NEFFECT*EUM*HRT"
sola<-pimdata(results=sol_NWEIA, outcome="NWE",sol = 1)
TH<-theory.evaluation(theory=t,empirics=sol_NWEIA,outcome = "NWE", sol=1, print.data=TRUE, print.fit=TRUE)
TH$cases
TH$fit

pima<-sol_NWEIA[["i.sol"]][["C1P1"]][["solution"]][[1]]

# To plot the paths:
View(sol_NWEIA)
sol_NWEIA$tt$initial.data$X<-NULL
sol_NWEIA$tt$initial.data$COUNTRY<-NULL
sol_NWEIA$tt$initial.data$YEAR<-NULL

XYplot(sol_NWEIA$i.sol$C1P1$solution, NWE,data=panelNWE, enhance=TRUE, clabels=rownames(panelNWE))
xy.plot("HPM*EUM*~HPF*~HSO", "NWE", data=PANELNWE, main="Sufficiency for NWE", ylab = "NWE", xlab="HPM*EUM*~HPF*~HSO")
XYplot(sol_NWEIA$i.sol$C1P2$solution, NWE,data=panelNWE, enhance=TRUE, clabels=rownames(panelNWE))
XYplot(sol_NWEIA$i.sol$C1P3$solution, NWE,data=panelNWE, enhance=TRUE, clabels=rownames(panelNWE))
XYplot(sol_NWEIA$i.sol$C1P4$solution, NWE,data=panelNWE, enhance=TRUE, clabels=rownames(panelNWE))

XYplot(sol_NWEIA$i.sol$C1P1$solution, NWE,data=panelNWE, enhance=TRUE, cex=0.6, clabels=rownames(panelNWE))
XYplot(sol_NWEIA$i.sol$C1P3$solution, NWE,data=panelNWE, enhance=TRUE, cex=0.6, clabels=rownames(panelNWE))


t<-"HPM*GDP*HPF*HSO*NCOR*NEFFECT*EUM*HRT"
sol_NWEIA
TH<-theory.evaluation(theory=t,empirics=sol_NWEIA,outcome = "NWE", sol=1, print.data=TRUE, print.fit=TRUE)

modelFit(model = sol_NWEIA, theory="HPM*GDP*HPF*HSO*NCOR*NEFFECT*EUM*HRT")

# To check for necessary conditions: Presence of Outcome (Own-Account Workers)

pof("HPM <= OWN", data=panel, relation = "necessity")
pof("EUM <= OWN", data=panel, relation = "necessity")
pof("HRT <= OWN", data=panel, relation="necessity")
pof("GDP <= OWN", data=panel, relation="necessity")
pof("HPF <= OWN", data=panel, relation = "necessity")
pof("HSO <= OWN", data=panel, relation="necessity")
pof("NCOR <= OWN", data=panel, relation = "necessity")
pof("NEFFECT <= OWN", data=panel, relation = "necessity")

# To check for necessary conditions: Absence of Outcome (Own-Account Workers)

pof("~HPM <= OWN", data=panel, relation = "necessity")
pof("~EUM <= OWN", data=panel, relation = "necessity")
pof("~HRT <= OWN", data=panel, relation="necessity")
pof("~GDP <= OWN", data=panel, relation="necessity")
pof("~HPF <= OWN", data=panel, relation = "necessity")
pof("~HSO <= OWN", data=panel, relation="necessity")
pof("~NCOR <= OWN", data=panel, relation = "necessity")
pof("~NEFFECT <= OWN", data=panel, relation = "necessity")


# To check for necessary conditions: Presence of Outcome II (Female family workers)

pof("HPM <= FAM", data=panel, relation = "necessity")
pof("EUM <= FAM", data=panel, relation = "necessity")
pof("HRT <= FAM", data=panel, relation="necessity")
pof("GDP <= FAM", data=panel, relation="necessity")
pof("HPF <= FAM", data=panel, relation = "necessity")
pof("HSO <= FAM", data=panel, relation="necessity")
pof("NCOR <= FAM", data=panel, relation = "necessity")
pof("NEFFECT <= FAM", data=panel, relation = "necessity")


# To check for absence of necessary conditions: Absence of Outcome II (Female family workers)
pof("~HPM <= FAM", data=panel, relation = "necessity")
pof("~EUM <= FAM", data=panel, relation = "necessity")
pof("~HRT <= FAM", data=panel, relation="necessity")
pof("~GDP <= FAM", data=panel, relation="necessity")
pof("~HPF <= FAM", data=panel, relation = "necessity")
pof("~HSO <= FAM", data=panel, relation="necessity")
pof("~NCOR <= FAM", data=panel, relation = "necessity")
pof("~NEFFECT <= FAM", data=panel, relation = "necessity")

# To check for necessary conditions: Presence and Absence of Outcome III (Female Employees)

pof("HPM <= EMPE", data=panel, relation = "necessity")
pof("EUM <= EMPE", data=panel, relation = "necessity")
pof("HRT <= EMPE", data=panel, relation="necessity")
pof("GDP <= EMPE", data=panel, relation="necessity")
pof("HPF <= EMPE", data=panel, relation = "necessity")
pof("HSO <= EMPE", data=panel, relation="necessity")
pof("NCOR <= EMPE", data=panel, relation = "necessity")
pof("NEFFECT <= EMPE", data=panel, relation = "necessity")
pof("~HPM <= EMPE", data=panel, relation = "necessity")
pof("~EUM <= EMPE", data=panel, relation = "necessity")
pof("~HRT <= EMPE", data=panel, relation="necessity")
pof("~GDP <= EMPE", data=panel, relation="necessity")
pof("~HPF <= EMPE", data=panel, relation = "necessity")
pof("~HSO <= EMPE", data=panel, relation="necessity")
pof("~NCOR <= EMPE", data=panel, relation = "necessity")
pof("~NEFFECT <= EMPE", data=panel, relation = "necessity")

# To check for necessary conditions: and Absence of Outcome IV (Female Employers)

pof("HPM <= EMPR", data=panel, relation = "necessity")
pof("EUM <= EMPR", data=panel, relation = "necessity")
pof("HRT <= EMPR", data=panel, relation="necessity")
pof("GDP <= EMPR", data=panel, relation="necessity")
pof("HPF <= EMPR", data=panel, relation = "necessity")
pof("HSO <= EMPR", data=panel, relation="necessity")
pof("NCOR <= EMPR", data=panel, relation = "necessity")
pof("NEFFECT <= EMPR", data=panel, relation = "necessity")
pof("~HPM <= EMPR", data=panel, relation = "necessity")
pof("~EUM <= EMPR", data=panel, relation = "necessity")
pof("~HRT <= EMPR", data=panel, relation="necessity")
pof("~GDP <= EMPR", data=panel, relation="necessity")
pof("~HPF <= EMPR", data=panel, relation = "necessity")
pof("~HSO <= EMPR", data=panel, relation="necessity")
pof("~NCOR <= EMPR", data=panel, relation = "necessity")
pof("~NEFFECT <= EMPR", data=panel, relation = "necessity")


# To test for sufficiency of female own-account workers:
ttOW <- truthTable(panel, conditions= "HPM,EUM,HRT,GDP,HPF,HSO,NCOR,NEFFECT",outcome="OWN", incl.cut = 0.90, sort.by = "incl,n", dcc= TRUE, decreasing=FALSE,complete=TRUE, show.cases = TRUE)
ttOW

# For the conservative solution of the sufficiency result:
sol_OWC <- minimize(ttOW, details=TRUE)
sol_OWC
sol_OWC$PIchart
pimplot(data=panel, results = sol_OWC, incl.tt = 0.9, outcome = "OWN", jitter=TRUE)

# For the parsimonious solution of the sufficiency result:
sol_OWP <- minimize(ttOW, include = "?", details=TRUE)
sol_OWP
names(sol_OWP)
sol_OWP$PIchart
sol_OWP$SA
pimplot(data=panel, results = sol_OWP, incl.tt = 0.9, outcome = "OWN", jitter=TRUE)

# For the intermediate solution and its logical minimization:
sol_OWI <- minimize(ttOW, include = "?", dir.exp = "1,1,1,1,1,1,1,1",details=TRUE)
sol_OWI
names(sol_OWI$i.sol)

# Standard Analysis:
sol_OWI$i.sol$C1P1$EC
sol_OWI$i.sol$C1P1$DC
pimplot(data=panel, results = sol_OWI, incl.tt = 0.9, outcome = "OWN", jitter=TRUE)

# For the enhanced standard analysis:
ESAOW <- findRows(obj = ttOW, type = 2)
ESAOW
ESAOWB <- findRows(obj=ttOW, type = 3)
ESAOWB
#ESA<-c(ESAOW,ESAOWB)
#ESA

# For the enhanced parsimonious solution:
sol_OPESA <- minimize(ttOW, include = "?", exclude=ESAOW, details=TRUE)
sol_OPESA

# For the enhanced intermediate solution:
sol_OIESA <- minimize(ttOW, include = "?", exclude= ESAOW, dir.exp ="1,1,1,1,1,1,1,1",details=TRUE)
sol_OIESA
pimplot(data=panel, results = sol_OIESA, incl.tt = 0.9, outcome = "OWN", jitter=TRUE)

# For the logical minimization of the enhanced intermediate solution:
names(sol_OIESA)
names(sol_OIESA$i.sol)
posOESA <- sol_OIESA$i.sol$C1P1$pims
posOESA
posOESB<-sol_OIESA$i.sol$C1P2$pims
posOESB
posOESC<-sol_OIESA$i.sol$C1P3$pims
posOESC
posOESD<-sol_OIESA$i.sol$C1P4$pims
posOESD

#Creating a new dataset:
panelOWN<- cbind(panel, posOESA,posOESB,posOESC,posOESD)

# For the sufficiency analysis:
OWNSA<-cluster(data = panelOWN, results = "~HPM*~EUM*GDP*HPF*NEFFECT", outcome = "OWN",unit_id = "COUNTRY", cluster_id = "YEAR", wicons=TRUE)
OWNSA
cluster.plot(OWNSA,labs=TRUE,size=8,angle=6,wicons=FALSE)
OWNSB<-cluster(data = panelOWN, results = "HPM*EUM*HRT*GDP*~HPF*~NCOR", outcome = "OWN",unit_id = "COUNTRY", cluster_id = "YEAR", wicons=TRUE)
OWNSB
OWNSC<-cluster(data = panelOWN, results = "HPM*EUM*HRT*GDP*HSO*~NEFFECT", outcome = "OWN",unit_id = "COUNTRY", cluster_id = "YEAR", wicons=TRUE)
OWNSC
HPM*EUM*GDP*~HPF*HSO*~NCOR 
~HPM*GDP*HPF*HSO*NCOR*NEFFECT
HPM*EUM*~HRT*~GDP*HSO*NCOR*~NEFFECT
HPM*~HRT*~GDP*~HPF*~HSO*~NCOR*~NEFFECT 
~HPM*~EUM*GDP*HPF*NEFFECT 
HPM*EUM*HRT*GDP*~HPF*~NCOR 
HPM*EUM*HRT*GDP*HSO*~NEFFECT 
HPM*EUM*GDP*HSO*~NCOR*~NEFFECT 
~HPM*GDP*HPF*HSO*NCOR*NEFFECT 
HPM*EUM*~HRT*~GDP*HSO*NCOR*~NEFFECT 
HPM*~HRT*~GDP*~HPF*~HSO*~NCOR*~NEFFECT 
~HPM*~EUM*GDP*HPF*NEFFECT 
HPM*EUM*HRT*GDP*HSO*~NEFFECT 
HPM*EUM*HRT*GDP*~NCOR*~NEFFECT 
HPM*EUM*GDP*~HPF*HSO*~NCOR 
~HPM*GDP*HPF*HSO*NCOR*NEFFECT 
HPM*EUM*~HRT*~GDP*HSO*NCOR*~NEFFECT 
HPM*~HRT*~GDP*~HPF*~HSO*~NCOR*~NEFFECT 
~HPM*~EUM*GDP*HPF*NEFFECT 
HPM*EUM*HRT*GDP*HSO*~NEFFECT 
HPM*EUM*HRT*GDP*~NCOR*~NEFFECT 
HPM*EUM*GDP*HSO*~NCOR*~NEFFECT 
~HPM*GDP*HPF*HSO*NCOR*NEFFECT 
HPM*EUM*~HRT*~GDP*HSO*NCOR*~NEFFECT 
HPM*~HRT*~GDP*~HPF*~HSO*~NCOR*~NEFFECT 




# To plot the paths:
View(sol_OIESA)
sol_OIESA$tt$initial.data$X<-NULL
sol_OIESA$tt$initial.data$COUNTRY<-NULL
sol_OIESA$tt$initial.data$YEAR<-NULL
XYplot(sol_OIESA$i.sol$C1P1$solution, OWN,data=panel, enhance=TRUE, clabels=rownames(panel))
XYplot(sol_OIESA$i.sol$C1P2$solution, OWN,data=panel, enhance=TRUE, clabels=rownames(panel))
XYplot(sol_OIESA$i.sol$C1P3$solution, OWN,data=panel, enhance=TRUE, clabels=rownames(panel))
XYplot(sol_OIESA$i.sol$C1P4$solution, OWN,data=panel, enhance=TRUE, clabels=rownames(panel))


## Sufficiency Analysis for the absence of the outcome:
ttowb <- truthTable(panel, conditions= "HPM,EUM,HRT,GDP,HPF,HSO,NCOR,NEFFECT",outcome="~OWN", incl.cut = 0.97, sort.by = "incl,n", dcc= TRUE,decreasing=FALSE,complete=TRUE, show.cases = TRUE)
ttowb
## Logical Minimization:
sol_owc <- minimize(ttowb, details=TRUE)
sol_owc
sol_owc$PIchart
pimplot(data=panel, results = sol_owc, incl.tt = 0.97, outcome = "~OWN", jitter=TRUE)
sol_owp <- minimize(ttowb, include = "?", details=TRUE)
sol_owp
names(sol_owp)
sol_owp$PIchart
sol_owp$SA
pimplot(data=panel, results = sol_owp, incl.tt = 0.97, outcome = "~OWN", jitter=TRUE)
sol_owi <- minimize(ttowb, include = "?", dir.exp = "0,0,0,0,0,0,0,0",details=TRUE)
names(sol_owi$i.sol)
# Standard Analysis:
sol_owi$i.sol$C1P1$EC
sol_owi$i.sol$C1P1$DC
pimplot(data=panel, results = sol_OWI, incl.tt = 0.9, outcome = "OWN", jitter=TRUE)

# For the enhanced standard analysis:
esaow <- findRows(obj = ttowb, type = 2)
esaow
esaowb <- findRows(obj=ttowb, type = 3)
esaowb
#ESA<-c(ESAOW,ESAOWB)
#ESA

# For the enhanced parsimonious solution:
sol_opesa <- minimize(ttowb, include = "?", exclude=esaow, details=TRUE)
sol_opesa

# For the enhanced intermediate solution:
sol_oiesa <- minimize(ttowb, include = "?", exclude= esaow, dir.exp ="0,0,0,0,0,0,0,0",details=TRUE)
sol_oiesa
pimplot(data=panel, results = sol_oiesa, incl.tt = 0.97, outcome = "~OWN", jitter=TRUE)

# For the logical minimization of the enhanced intermediate solution:
names(sol_oiesa)
names(sol_oiesa$i.sol)
posown<-sol_oiesa$i.sol$C1P1$pims
panelown <- cbind(panel, posown)

LR.intersect(sol_OWI,sol_owi)

# To test for sufficiency of female family workers:
ttFM <- truthTable(panel, conditions= "HPM,EUM,HRT,GDP,HPF,HSO,NCOR,NEFFECT",outcome="FAM", incl.cut = 0.85, sort.by = "incl,n", dcc= TRUE, decreasing=FALSE,complete=TRUE, show.cases = TRUE)
ttFM

# For the conservative solution of the sufficiency result:
sol_FM <- minimize(ttFM, details=TRUE)
sol_FM
sol_FM$PIchart
pimplot(data=panel, results = sol_FM, incl.tt = 0.85, outcome = "FAM", jitter=TRUE)

# For the parsimonious solution of the sufficiency result:
sol_FMP <- minimize(ttFM, include = "?", details=TRUE)
sol_FMP
names(sol_FMP)
sol_FMP$PIchart
sol_FMP$SA
pimplot(data=panel, results = sol_FMP, incl.tt = 0.85, outcome = "FAM", jitter=TRUE)

# For the intermediate solution and its logical minimization:
sol_FMI <- minimize(ttFM, include = "?", dir.exp = "1,1,1,1,1,1,1,1",details=TRUE)
sol_FMI
names(sol_FMI$i.sol)

# Standard Analysis:
sol_FMI$i.sol$C1P1$EC
sol_FMI$i.sol$C1P1$DC
pimplot(data=panel, results = sol_FMI, incl.tt = 0.85, outcome = "FAM", jitter=TRUE)

# For the enhanced standard analysis, no error 3:
ESFM <- findRows(obj = ttFM, type = 2)
ESFM
ESFMB <- findRows(obj=ttFM, type = 3)
ESFMB

# For the enhanced parsimonious solution:
sol_EPSFM<- minimize(ttFM, include = "?", exclude=ESFM, details=TRUE)
sol_EPSFM

# For the enhanced intermediate solution:
sol_EIFM <- minimize(ttFM, include = "?", exclude= ESFM, dir.exp ="1,1,1,1,1,1,1,1",details=TRUE)
sol_EIFM
pimplot(data=panel, results = sol_EIFM, incl.tt = 0.85, outcome = "FAM", jitter=TRUE)

# For the logical minimization of the enhanced intermediate solution:
names(sol_EIFM)
names(sol_EIFM$i.sol)
posEIFM <- sol_EIFM$i.sol$C1P1$pims
posEIFM

#Creating a new dataset:
panelFAM<- cbind(panel,posEIFM)

# For the sufficiency analysis:
OWNS<-cluster(data = panelESAO, results = "~HRT*GDP*~HPF*~GI*NEFFECT", outcome = "OWN",unit_id = "COUNTRY", cluster_id = "YEAR", wicons=TRUE)
OWNS
cluster.plot(cluster.res=CBA, labs=TRUE,size=8,angle=6,wicons=TRUE)

# To plot the paths:
View(sol_EIFM)
sol_EIFM$tt$initial.data$X<-NULL
sol_EIFM$tt$initial.data$COUNTRY<-NULL
sol_EIFM$tt$initial.data$YEAR<-NULL
XYplot(sol_EIFM$i.sol$C1P1$solution, FAM,data=panel, enhance=TRUE, clabels=rownames(panel))
XYplot(sol_EIFM$i.sol$C1P2$solution, FAM,data=panel, enhance=TRUE, clabels=rownames(panel))
XYplot(sol_EIFM$i.sol$C1P3$solution, FAM,data=panel, enhance=TRUE, clabels=rownames(panel))
XYplot(sol_EIFM$i.sol$C1P4$solution, FAM,data=panel, enhance=TRUE, clabels=rownames(panel))

## Sufficiency Analysis for the absence of the outcome:
ttfmb <- truthTable(panel, conditions= "HPM,EUM,HRT,GDP,HPF,HSO,NCOR,NEFFECT",outcome="~FAM", incl.cut = 0.9, sort.by = "incl,n", dcc= TRUE,decreasing=FALSE,complete=TRUE, show.cases = TRUE)
ttfmb

## Logical Minimization:
sol_fmb <- minimize(ttfmb, details=TRUE)
sol_fmb
sol_fmb$PIchart
pimplot(data=panel, results = sol_fmb, incl.tt = 0.9, outcome = "FAM", jitter=TRUE)
sol_fmp <- minimize(ttfmb, include = "?", details=TRUE)
sol_fmp
names(sol_fmp)
sol_fmp$PIchart
sol_fmp$SA
pimplot(data=panel, results = sol_fmp, incl.tt = 0.9, outcome = "FAM", jitter=TRUE)
sol_fmi <- minimize(ttfmb, include = "?", dir.exp = "0,0,0,0,0,0,0,0",details=TRUE)
names(sol_fmi$i.sol)
# Standard Analysis:
sol_fmi$i.sol$C1P1$EC
sol_fmi$i.sol$C1P1$DC
pimplot(data=panel, results = sol_fmi, incl.tt = 0.9, outcome = "FAM", jitter=TRUE)

# For the enhanced standard analysis, no error 3:
esfm <- findRows(obj = ttfmb, type = 2)
esfm
esfmb <- findRows(obj=ttfmb, type = 3)
esfmb

# For the enhanced parsimonious solution:
sol_epfm <- minimize(ttfmb, include = "?", exclude=esfm, details=TRUE)
sol_epfm

# For the enhanced intermediate solution:
sol_esfi <- minimize(ttfmb, include = "?", exclude= esfm, dir.exp ="0,0,0,0,0,0,0,0",details=TRUE)
sol_esfi
pimplot(data=panel, results = sol_esfi, incl.tt = 0.9, outcome="FAM", jitter=TRUE)

LR.intersect(sol_EIFM, sol_esfi)

# For the logical minimization of the enhanced intermediate solution:
names(sol_esfi)
names(sol_esfi$i.sol)
posfam<-sol_esfi$i.sol$C1P1$pims
posfam
posfamb<-sol_esfi$i.sol$C1P2$pims
posfamb
posfamc<-sol_esfi$i.sol$C1P3$pims
posfamc
posfamd<-sol_esfi$i.sol$C1P7$pims
posfamd

panelfam <- cbind(panel, posfam, posfamb, posfamc, posfamd)


# To test for sufficiency of female employees:
ttEMPE <- truthTable(panel, conditions= "HPM,EUM,HRT,GDP,HPF,HSO,NCOR,NEFFECT",outcome="EMPE", incl.cut = 0.90, sort.by = "incl,n", dcc= TRUE, decreasing=FALSE,complete=TRUE, show.cases = TRUE)
ttEMPE

# For the conservative solution of the sufficiency result:
sol_EMPE <- minimize(ttEMPE, details=TRUE)
sol_EMPE
sol_EMPE$PIchart
pimplot(data=panel, results = sol_EMPE, incl.tt = 0.9, outcome = "EMPE", jitter=TRUE)

# For the parsimonious solution of the sufficiency result:
sol_EMP <- minimize(ttEMPE, include = "?", details=TRUE)
sol_EMP
names(sol_EMP)
sol_EMP$PIchart
sol_EMP$SA
pimplot(data=panel, results = sol_EMP, incl.tt = 0.9, outcome = "EMPE", jitter=TRUE)

# For the intermediate solution and its logical minimization:
sol_EMI <- minimize(ttEMPE, include = "?", dir.exp = "1,1,1,1,1,1,1,1",details=TRUE)
sol_EMI
names(sol_EMI$i.sol)

# Standard Analysis:
sol_EMI$i.sol$C1P1$EC
sol_EMI$i.sol$C1P1$DC
pimplot(data=panel, results = sol_EMI, incl.tt = 0.9, outcome = "EMPE", jitter=TRUE)

# For the enhanced standard analysis, no error 3:
ESEMP <- findRows(obj = ttEMPE, type = 2)
ESEMP
ESEMPB <- findRows(obj=ttEMPE, type = 3)
ESEMPB

# For the enhanced parsimonious solution:
sol_EPSEMP<- minimize(ttEMPE, include = "?", exclude=ESEMP, details=TRUE)
sol_EPSEMP

# For the enhanced intermediate solution:
sol_EISEMP <- minimize(ttEMPE, include = "?", exclude= ESEMP, dir.exp ="1,1,1,1,1,1,1,1",details=TRUE)
sol_EISEMP
pimplot(data=panel, results = sol_EISEMP, incl.tt = 0.9, outcome = "EMPE", jitter=TRUE)

# For the logical minimization of the enhanced intermediate solution:
names(sol_EISEMP)
names(sol_EISEMP$i.sol)
posEISEMPE <- sol_EISEMP$i.sol$C1P1$pims
posEISEMPE
posEISEMPEB <- sol_EISEMP$i.sol$C2P1$pims
posEISEMPEB

#Creating a new dataset:
panelEMPE<- cbind(panel,posEISEMPE,posEISEMPEB)

# For the sufficiency analysis:
OWNS<-cluster(data = panelESAO, results = "~HRT*GDP*~HPF*~GI*NEFFECT", outcome = "OWN",unit_id = "COUNTRY", cluster_id = "YEAR", wicons=TRUE)
OWNS
cluster.plot(cluster.res=CBA, labs=TRUE,size=8,angle=6,wicons=TRUE)

# To plot the paths:
View(sol_EISEMP)
sol_EISEMP$tt$initial.data$X<-NULL
sol_EISEMP$tt$initial.data$COUNTRY<-NULL
sol_EISEMP$tt$initial.data$YEAR<-NULL
XYplot(sol_EISEMP$i.sol$C1P1$solution[[1]], EMPE,data=panel, enhance=TRUE, jitter=TRUE, cex=0.5, clabels=rownames(panel))
XYplot(sol_EISEMP$i.sol$C1P1$solution[[2]], EMPE,data=panel, enhance=TRUE, jitter=TRUE, cex=0.5, clabels=rownames(panel))
XYplot(sol_EISEMP$i.sol$C2P1$solution, EMPE,data=panel, enhance=TRUE, jitter=TRUE, cex=0.5, clabels=rownames(panel))

## Sufficiency Analysis for the absence of the outcome:
ttempe <- truthTable(panel, conditions= "HPM,EUM,HRT,GDP,HPF,HSO,NCOR,NEFFECT",outcome="~EMPE", incl.cut = 0.99, sort.by = "incl,n", dcc= TRUE,decreasing=FALSE,complete=TRUE, show.cases = TRUE)
ttempe

## Logical Minimization:
sol_emp <- minimize(ttempe, details=TRUE)
sol_emp
sol_emp$PIchart
pimplot(data=panel, results = sol_emp, incl.tt = 0.9, outcome = "~EMPE", jitter=TRUE)
sol_emps <- minimize(ttempe, include = "?", details=TRUE)
sol_emps
names(sol_emps)
sol_emps$PIchart
sol_emps$SA
pimplot(data=panel, results = sol_emps, incl.tt = 0.9, outcome = "~EMPE", jitter=TRUE)
sol_empi <- minimize(ttempe, include = "?", dir.exp = "0,0,0,0,0,0,0,0",details=TRUE)
names(sol_empi$i.sol)
# Standard Analysis:
sol_empi$i.sol$C1P1$EC
sol_empi$i.sol$C1P1$DC
pimplot(data=panel, results = sol_empi, incl.tt = 0.9, outcome = "~EMPE", jitter=TRUE)

# For the enhanced standard analysis, no error 3:
esempe <- findRows(obj = ttempe, type = 2)
esempe
esempeb <- findRows(obj=ttempe, type = 3)
esempeb


# For the enhanced parsimonious solution:
sol_epfm <- minimize(ttempe, include = "?", exclude=esempe, details=TRUE)
sol_epfm

# For the enhanced intermediate solution:
sol_epfi <- minimize(ttempe, include = "?", exclude= esempe, dir.exp ="0,0,0,0,0,0,0,0",details=TRUE)
sol_epfi
pimplot(data=panel, results = sol_epfi, incl.tt = 0.9, outcome="~EMPE", jitter=TRUE)

LR.intersect(sol_EISEMP, sol_epfi)

# For the logical minimization of the enhanced intermediate solution:
names(sol_epfi)
names(sol_epfi$i.sol)
posempe<-sol_epfi$i.sol$C1P1$pims
posempe
panelempe <- cbind(panel, posempe)

# To test for sufficiency of female employers:
ttEMPR <- truthTable(panel, conditions= "HPM,EUM,HRT,GDP,HPF,HSO,NCOR,NEFFECT",outcome="EMPR", incl.cut = 0.90, sort.by = "incl,n", dcc= TRUE, decreasing=FALSE,complete=TRUE, show.cases = TRUE)
ttEMPR

# For the conservative solution of the sufficiency result:
sol_EMPR <- minimize(ttEMPR, details=TRUE)
sol_EMPR
sol_EMPR$PIchart
pimplot(data=panel, results = sol_EMPR, incl.tt = 0.9, outcome = "EMPR", jitter=TRUE)

# For the parsimonious solution of the sufficiency result:
sol_EMPRP <- minimize(ttEMPR, include = "?", details=TRUE)
sol_EMPRP
names(sol_EMPRP)
sol_EMPRP$PIchart
sol_EMPRP$SA
pimplot(data=panel, results = sol_EMPRP, incl.tt = 0.9, outcome = "EMPR", jitter=TRUE)

# For the intermediate solution and its logical minimization:
sol_EMPRI <- minimize(ttEMPR, include = "?", dir.exp = "1,1,1,1,1,1,1,1",details=TRUE)
sol_EMPRI
names(sol_EMPRI$i.sol)

# Standard Analysis:
sol_EMPRI$i.sol$C1P1$EC
sol_EMPRI$i.sol$C1P1$DC
pimplot(data=panel, results = sol_EMPRI, incl.tt = 0.9, outcome = "EMPR", jitter=TRUE)

# For the enhanced standard analysis:
ESEMPR <- findRows(obj = ttEMPR, type = 2)
ESEMPR
ESEMPRB <- findRows(obj=ttEMPR, type = 3)
ESEMPRB

# For the enhanced parsimonious solution:
sol_EPSEMPR<- minimize(ttEMPR, include = "?", exclude=ESEMPR, details=TRUE)
sol_EPSEMPR

# For the enhanced intermediate solution:
sol_EISEMPR <- minimize(ttEMPR, include = "?", exclude= ESEMPR, dir.exp ="1,1,1,1,1,1,1,1",details=TRUE)
sol_EISEMPR
pimplot(data=panel, results = sol_EISEMPR, incl.tt = 0.9, outcome = "EMPR", jitter=TRUE)

# For the logical minimization of the enhanced intermediate solution:
names(sol_EISEMPR)
names(sol_EISEMPR$i.sol)
posEISEMPR <- sol_EISEMPR$i.sol$C1P1$pims
posEISEMPR

#Creating a new dataset:
panelEMPR<- cbind(panel,posEISEMPR)

# For the sufficiency analysis:
OWNS<-cluster(data = panelESAO, results = "~HRT*GDP*~HPF*~GI*NEFFECT", outcome = "OWN",unit_id = "COUNTRY", cluster_id = "YEAR", wicons=TRUE)
OWNS
cluster.plot(cluster.res=CBA, labs=TRUE,size=8,angle=6,wicons=TRUE)

# To plot the paths:
View(sol_EISEMPR)
sol_EISEMPR$tt$initial.data$X<-NULL
sol_EISEMPR$tt$initial.data$COUNTRY<-NULL
sol_EISEMPR$tt$initial.data$YEAR<-NULL
XYplot(sol_EISEMPR$i.sol$C1P1$solution, EMPR,data=panel, enhance=TRUE, cex=0.5, clabels=rownames(panel))
XYplot(sol_EISEMPR$i.sol$C2P1$solution, EMPR,data=panel, enhance=TRUE, cex=0.5, clabels=rownames(panel))

## Sufficiency Analysis for the absence of the outcome:
ttempr <- truthTable(panel, conditions= "HPM,EUM,HRT,GDP,HPF,HSO,NCOR,NEFFECT",outcome="~EMPR", incl.cut = 0.97, sort.by = "incl,n", dcc= TRUE,decreasing=FALSE,complete=TRUE, show.cases = TRUE)
ttempr

## Logical Minimization:
sol_empr <- minimize(ttempr, details=TRUE)
sol_empr
sol_empr$PIchart
pimplot(data=panel, results = sol_empr, incl.tt = 0.97, outcome = "~EMPR", jitter=TRUE)
sol_empsp <- minimize(ttempr, include = "?", details=TRUE)
sol_empsp
names(sol_empsp)
sol_empsp$PIchart
sol_empsp$SA
pimplot(data=panel, results = sol_empsp, incl.tt = 0.97, outcome = "~EMPR", jitter=TRUE)
sol_empir <- minimize(ttempr, include = "?", dir.exp = "0,0,0,0,0,0,0,0",details=TRUE)
names(sol_empir$i.sol)
# Standard Analysis:
sol_empir$i.sol$C1P1$EC
sol_empir$i.sol$C1P1$DC
pimplot(data=panel, results = sol_empir, incl.tt = 0.97, outcome = "~EMPR", jitter=TRUE)

# For the enhanced standard analysis:
esemper <- findRows(obj = ttempr, type = 2)
esemper
esemperb <- findRows(obj=ttempr, type = 3)
esemperb


# For the enhanced parsimonious solution:
sol_eprm <- minimize(ttempr, include = "?", exclude=esemper, details=TRUE)
sol_eprm

# For the enhanced intermediate solution:
sol_epri <- minimize(ttempr, include = "?", exclude= esemper, dir.exp ="0,0,0,0,0,0,0,0",details=TRUE)
sol_epri
pimplot(data=panel, results = sol_epri, incl.tt = 0.97, outcome="~EMPR", jitter=TRUE)

LR.intersect(sol_EISEMPR, sol_epri)

# For the logical minimization of the enhanced intermediate solution:
names(sol_epri)
names(sol_epri$i.sol)
posempr<-sol_epri$i.sol$C1P1$pims
posempr
panelempr <- cbind(panel, posempr)





